package com.cqut.controller;

import com.cqut.VO.result.ResultVO;
import com.cqut.entity.FileAttachment;
import com.cqut.entity.FileDirectory;
import com.cqut.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("//file")
public class FileController {

    @Autowired
    FileService service;

    /**
     * 下载文件接口
     * @param userName 用户名
     * @param fileName 文件名称
     * @return 文件的二进制流
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam String userName,@RequestParam String fileName){
        return service.downloadFile(userName,fileName);
    }

    /**
     * 上传文件接口
     * @param file MultipartFile 文件体
     * @param userName 用户名
     * @param dirName 目录名
     * @return 上传的成功与否
     */
    @RequestMapping("/upload")
    public ResultVO<Boolean> uploadFile(@RequestParam MultipartFile file,@RequestParam String userName,@RequestParam String dirName){
        return service.uploadFile(file,userName,dirName);
    }

    /**
     * 创建目录接口
     * @param userName 用户名
     * @param dirName 目录名
     * @return 创建的成功与否
     */
    @RequestMapping("/createDirectory")
    public ResultVO<Boolean> createDir(@RequestParam String userName,@RequestParam String dirName,@RequestParam String parentDirName){
        return service.createDir(userName,dirName,parentDirName);
    }


    @RequestMapping("/deleteFile")
    public ResultVO<Boolean> deleteFile(@RequestParam String userName,@RequestParam String dirPath,@RequestParam String fileName){
        return service.deleteFile(userName,dirPath,fileName);
    }

    @RequestMapping("/deleteDir")
    public ResultVO<Boolean> deleteDir(@RequestParam String userName,@RequestParam String dirName){
        return service.deleteDir(userName,dirName);
    }

    /**
     * 重命名或移动一个文件夹/文件
     * @param userName
     * @param originalDir
     * @param replaceDir
     * @return
     */
    @RequestMapping("/moveIo")
    public ResultVO<Boolean> moveDirOrFile(@RequestParam String userName,@RequestParam String originalDir,@RequestParam String replaceDir){
        return service.moveIo(userName, originalDir, replaceDir);
    }

    @RequestMapping("/getFileList")
    public ResultVO<List<FileAttachment>> getFileList(@RequestParam String userName,@RequestParam String dirName){
        return service.getFileList(userName,dirName);
    }

    @RequestMapping("/getDirList")
    public ResultVO<List<FileDirectory>> getDirList(@RequestParam String userName, @RequestParam String dirName){
        return service.getDirList(userName,dirName);
    }
}
